package com.mark.mall.common.resultBean;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Collection;

/**
 * @author admin
 */
@ApiModel(description = "通用响应返回对象")
@Data
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    public static final int NO_LOGIN = -1;
    public static final int SUCCESS = 0;
    public static final int FAIL = 1;
    public static final int NO_PERMISSION = 2;
    public static final int DOCKING_FAIL = 3;
    /**
     * 仅GET请求返回message会是 success
     */
    public static final String SUCCESS_STR = "success";

    public static final String SUCCESS_COMMIT_STR = "操作成功！";

    @ApiModelProperty(value = "提示信息")
    private String msg = SUCCESS_STR;

    @ApiModelProperty(value = "结果代码", example = "0")
    private int code = SUCCESS;

    @ApiModelProperty(value = "结果数据")
    private T data;

    @ApiModelProperty("数据总量")
    private Long count;

    @ApiModelProperty("备用字段")
    private Object more;

    public R() {
        super();
    }

    public R(T data) {
        super();
        this.data = data;
        if (data instanceof Collection) {
            this.count = (long) ((Collection) data).size();
        }
        if (data instanceof Object[]) {
            this.count = (long) ((Object[]) data).length;
        }
    }

    public R(T data, long count) {
        super();
        this.data = data;
        this.count = count;
    }

    public R(T data, String msg) {
        this.msg = msg;
        this.data = data;
    }

    public R(T data, boolean needCommit) {
        this.msg = needCommit ? SUCCESS_COMMIT_STR : SUCCESS_STR;
        this.data = data;
    }

    public R(String msg) {
        super();
        this.msg = msg;
        this.code = FAIL;
    }

    public R(Throwable e) {
        super();
        this.msg = e.toString();
        this.code = FAIL;
    }
}
